package common

/**
* Created by LONG  on 2018/5/31.
 */

import (
	"TeacherManager/system"

	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

type ExamQuestions struct {
	Id        bson.ObjectId `bson:"_id"`
	ExamID    int           `bson:"examid"`
	Questions []Expresstion `bson:"questions"` //bson:"questions" 表示mongodb数据库中对应的字段名称
	QNums     int           `bson:"qnums"`
}

var URL string //mongodb连接字符串
func Init() {
	URL = system.GetConfiguration().MongoDsn
}

var (
	mgoSession *mgo.Session
	dataBase   = "Questions"
)

/**
 * 公共方法，获取session，如果存在则拷贝一份
 */
func getSession() *mgo.Session {
	if mgoSession == nil {
		var err error
		mgoSession, err = mgo.Dial(URL)
		if err != nil {
			panic(err) //直接终止程序运行
		}
	}
	//最大连接池默认为4096
	return mgoSession.Clone()
}

//公共方法，获取collection对象
func witchCollection(collection string, s func(*mgo.Collection) error) error {
	session := getSession()
	defer session.Close()
	c := session.DB(dataBase).C(collection)
	return s(c)
}

/**
 * 添加ExamQuestions对象
 */
func AddExamQuestions(e ExamQuestions) error {
	e.Id = bson.NewObjectId()
	query := func(c *mgo.Collection) error {
		return c.Insert(e)
	}
	err := witchCollection("questions", query)
	if err != nil {
		return err
	}
	return nil
}

/**
 * 获取一条记录通过objectid
 */
func GetExamQuestionsByObjId(id string) *ExamQuestions {
	objid := bson.ObjectIdHex(id)
	examQuestions := new(ExamQuestions)
	query := func(c *mgo.Collection) error {
		return c.FindId(objid).One(&examQuestions)
	}
	witchCollection("questions", query)
	return examQuestions
}

func GetExamQuestionsByExamId(id int) (*ExamQuestions, error) {
	examQuestions := new(ExamQuestions)
	query := func(c *mgo.Collection) error {
		return c.Find(bson.M{"examid": id}).One(&examQuestions)
	}
	err := witchCollection("questions", query)
	return examQuestions, err
}

//获取所有的person数据
func PageExamQuestions() []ExamQuestions {
	var examQuestions []ExamQuestions
	query := func(c *mgo.Collection) error {
		return c.Find(nil).All(&examQuestions)
	}
	err := witchCollection("questions", query)
	if err != nil {
		return examQuestions
	}
	return examQuestions
}

//更新person数据
func UpdateExamQuestions(eid int, questions []Expresstion) error {
	query := bson.M{"examid": eid}
	change := bson.M{"questions": questions, "qnums": len(questions), "examid": eid}
	exop := func(c *mgo.Collection) error {
		return c.Update(query, change)
	}
	err := witchCollection("questions", exop)
	if err != nil {
		return err
	}
	return nil
}

/**
 * 执行查询，此方法可拆分做为公共方法
 * [SearchPerson description]
 * @param {[type]} collectionName string [description]
 * @param {[type]} query          bson.M [description]
 * @param {[type]} sort           bson.M [description]
 * @param {[type]} fields         bson.M [description]
 * @param {[type]} skip           int    [description]
 * @param {[type]} limit          int)   (results      []interface{}, err error [description]
 */
func SearchExamQuestions(collectionName string, query bson.M, sort string, fields bson.M, skip int, limit int) (results []interface{}, err error) {
	exop := func(c *mgo.Collection) error {
		return c.Find(query).Sort(sort).Select(fields).Skip(skip).Limit(limit).All(&results)
	}
	err = witchCollection(collectionName, exop)
	return
}
